package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.FlowerDTO;
import io.github.talelin.latticy.entity.Flower;
import io.github.talelin.latticy.mapper.FlowerMapper;
import io.github.talelin.latticy.model.FlowerDO;
import io.github.talelin.latticy.repository.FlowerRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowerService extends ServiceImpl<FlowerMapper, FlowerDO>
{
    @Autowired
    private FlowerRepository flowerRepository;

    public List<Flower> getAll()
    {
        return flowerRepository.findAll();
    }


    /******************************* 后台 **********************************/
    public void create(FlowerDTO dto)
    {
        FlowerDO flowerDO = new FlowerDO();
        BeanUtils.copyProperties(dto, flowerDO);
        this.save(flowerDO);
    }

    public FlowerDO get(Long id)
    {
        FlowerDO flowerDO = this.getById(id);
        if (flowerDO == null) {
            throw new NotFoundException();
        }
        return flowerDO;
    }

    public void update(Long id, FlowerDTO dto)
    {
        FlowerDO flowerDO = this.get(id);
        BeanUtils.copyProperties(dto, flowerDO);
        this.getBaseMapper().updateById(flowerDO);
    }

    public void delete(Long id)
    {
        this.get(id);
        this.getBaseMapper().deleteById(id);
    }


}
